﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace scmsByAspdNet.cotro.teacher {
  /// <summary>
  /// getAllTeacherCourseNew 的摘要说明
  /// </summary>
  public class getAllTeacherCourseNew : IHttpHandler {

    public void ProcessRequest(HttpContext context) {
      context.Response.ContentType = "text/plain";
      //获取请求参数
      string tea_id = context.Request["tea_id"];
      //数据库
      SqlConnection conn = new SqlConnection("Data Source=106.54.69.143;database=scms;uid=sa;pwd=Aspdotnet666");
      conn.Open();
      //所需sql代码
      SqlCommand sql_1 = new SqlCommand("SELECT sc.id, sc.title " +
        " FROM sc_teacher st, sc_course sc, sc_cour_tea sct " +
        " WHERE st.id='"+tea_id+"' AND " +
        " st.id=sct.tea_id AND " +
        " sct.cour_id=sc.id AND " +
        " sc.is_deleted=0 AND" +
        " sct.is_deleted=0", conn);
      //获取个数
      SqlCommand sql_count=new SqlCommand("SELECT count(sc.id)" +
        " FROM sc_teacher st, sc_course sc, sc_cour_tea sct " +
        " WHERE st.id='" + tea_id + "' AND " +
        " st.id=sct.tea_id AND " +
        " sct.cour_id=sc.id AND " +
        " sc.is_deleted=0 AND" +
        " sct.is_deleted=0", conn);

      string rows = "[";
      //获取行
      //sql_1.ExecuteNonQuery();
      SqlDataReader reader = sql_1.ExecuteReader();
      for (; reader.Read();){
        rows += $"{{\"cour_id\":\"{reader[0].ToString()}\",\"cour_title\":\"{reader[1].ToString()}\"}},";
      }
      rows = rows.Substring(0, rows.Length - 1);
      rows += "]";
      reader.Close();
      //获取行数
      string total="";
      reader = sql_count.ExecuteReader();
      if (reader.Read()) {
        total = reader[0].ToString();
      }
      else {
        total = "0";
      }
      reader.Close();
      conn.Close();
      //返回数据
      string ret = "{" +
        "\"total\":\""+total+"\"," +
        "\"rows\":"+rows+"" +
        "}";
      System.Console.WriteLine(ret);
      context.Response.StatusCode = 200;
      context.Response.Write(ret);
    }

    public bool IsReusable {
      get {
        return false;
      }
    }
  }
}